<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use app\common\library\YuUtil;
use think\Exception;
use think\Db;


class Share extends IndexBase
{

    /**
     * @var 登录的用户id
     */
    protected $uid;
    /**
     * @var 登录的用户信息对象
     */
    protected $jsonObject;

    /**
     * @var 登录的token
     */
    protected $token;

    /**
     * @var array 前置操作，这里是检查登录
     */
    protected $beforeActionList = [
//        'checkAuth'=>['except'=>'usertree,usertree2,usertree3,autotitle,usertree5']
    ];

    /**
     * @title 检查登录
     * @description 检查登录
     * @author Admin ldy
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/19  9:36
     */
    protected function checkAuth(){
        /// 如需登录，调用此方法鉴权
        /// 注解1：如果已经登录，则会获取到uid;否则会直接打印错误信息，不执行下面的代码
        /// 注解2：该方法走的是缓存，不通过数据库检查
        /// 注解3：如果没有token信息，需要调登录接口，重新获取token，并保存，token有效期暂定为30天
        /// 注解4：该类所有方法需要登录鉴权，故预先加载
        $cookietoken = $this->request->param('token');
        $cookieuser = $this->request->param('userstring');
//        $cookietoken =cookie('scj_token');
//        $cookieuser =cookie('scj_user');
//        $cookietoken =cookie('scj_token');
//        $cookieuser =cookie('scj_user');

        try {
            //验证客服端是否有token
            if (YuUtil::is_empty($cookietoken)|| YuUtil::is_empty($cookieuser)) {
                $this->redirect('index/login');
            }
            //验证服务器中的token与客户端是否一致！！
            $this->checkUidByToken($cookietoken);
            //从客户端中获取用户信息？
            $jsonObject = json_decode($cookieuser);
            $this->uid = $jsonObject->id;
            $this->jsonObject = $jsonObject;
            $this->token = $cookietoken;
        }catch (Exception $e){
            echo $e->getTraceAsString();
        }
        //用户如果操作了个人的数据，就代表今天签到了
//        Sign::isSignToday($this->uid);
    }

    public function link(){
        //http://scj.web.co/index/share/link?id=103
        $id = $this->request->param('i');
        $where = array(
            'a.id'=>$id,
            'a.status_is_detele'=>0,
        );
        try{
            $arr = Db::table('scj_url_person')
                ->field('w.url,a.*')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where)
                ->select();
            $user = Db::table('scj_url_person')
                ->field('b.nickname,b.email,a.id')
                ->alias('a')
                ->join('scj_user b','a.user_id = b.id')
                ->where($where)
                ->select();
            $type = Db::table('scj_url_person')
                ->field('c.name,a.id')
                ->alias('a')
                ->join('scj_type c','a.type_id = c.id')
                ->where($where)
                ->select();
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
        if (empty($arr[0])){
            return '非法数据操作！';
        }
        if (empty($user)){
            $user['0']['email']='www.scj100.com';
        }
        if (empty($type)){
            $type['0']['name']='收藏家';
        }
        $this->view->assign('data',$arr['0']);
        $this->view->assign('user',$user['0']);
        $this->view->assign('type',$type['0']);
        return $this->view->fetch('Share/link');
        //后期并发大的时候，改成下面这个就可以全静态访问，性能大大的提升
//        return $this->autoChacheView('Share/link');
    }

    public function floder(){
        $typeid = $this->request->param('t');
        if (empty($typeid)){
            return '非法数据操作！';
        }
        $where = array(
            'id'=>$typeid,
        );
        try{
            $result = Db::table('scj_type')
                ->field('name,uid')
                ->where($where)
                ->find();
            if (empty($result)){
                return '非法数据操作！';
            }
            $where2 = array(
                'user_id'=>$result['uid'],
                'type_id'=>$typeid,
            );
            $result2 = Db::table('scj_url_person')
                ->field('w.url,a.*')
                ->alias('a')
                ->join('scj_url_public w','a.base_url_id = w.id')
                ->where($where2)
                ->select();
            $where3 = array(
                'pid'=>$typeid,
            );
            $re3  = Db::table('scj_type')
                ->field('*')
                ->where($where3)
                ->select();
        }catch (Exception $e){
            $this->err(ErrorType::ACTION_FAILED,$e->getTraceAsString());
        }
        $this->view->assign('title',$result['name']);
        $this->view->assign('list_floder',$re3);
        $this->view->assign('list',$result2);
        return $this->view->fetch('Share/floder');
//        return $this->autoChacheView('Share/floder');
    }

    public function te2(){
        echo \think\facade\App::version();
        echo '<br><br>';
        echo phpinfo();

//        return $this->view->fetch('Share/te2');

//        $des = new \app\common\library\DES3();
//        $string = '272604545@qq.com';
//        echo '原字符串： '.$string."<br>";
//        $s = $des->encrypt(null,$string);
//        echo '加密后： '.$s.'<br>';
//        $e = $des->decrypt(null,$s);
//        echo '解密后： '.$e.'<br>';
    }



}
